//
//  NYTopicPictureView.m
//  百思不得姐
//
//  Created by Apple on 15/8/24.
//  Copyright (c) 2015年 Neal. All rights reserved.
//

#import "NYTopicPictureView.h"
#import "NYTopicModel.h"
#import <UIImageView+WebCache.h>
#import "NYProgressView.h"
#import "NYShowPictureViewController.h"
@interface NYTopicPictureView ()

@property (weak, nonatomic) IBOutlet UIImageView *is_gifView;
@property (weak, nonatomic) IBOutlet UIButton *bigPictureButton;
/* progess */
@property(nonatomic , weak) IBOutlet NYProgressView *progressView;

@end
@implementation NYTopicPictureView

- (void)setTopic:(NYTopicModel *)topic
{
    [super setTopic:topic];
    
    self.progressView.progress = topic.pictureProgress;
    
    self.is_gifView.hidden = !topic.is_gif;

    if (topic.bigPicture) {
        self.bigPictureButton.hidden = NO;
        _pictureView.contentMode = UIViewContentModeTop;
        _pictureView.clipsToBounds = YES;
    } else {
        self.bigPictureButton.hidden = YES;
        _pictureView.contentMode = UIViewContentModeScaleToFill;
        _pictureView.clipsToBounds = NO;

        
    }
    
    [_pictureView sd_setImageWithURL:[NSURL URLWithString:topic.bigimage] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
        self.progressView.hidden = NO;
        self.progressView.progress = receivedSize*1.0 /expectedSize;
        topic.pictureProgress = self.progressView.progress;
        NSLog(@"%f",self.progressView.progress);
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        self.progressView.hidden = YES;
        topic.pictureProgress = 1.0;

        if (topic.isBigPicture == NO) {
            return ;
        }
        UIGraphicsBeginImageContext(_pictureView.frame.size);
        [image drawInRect:CGRectMake(0, 0, _pictureView.width, _pictureView.width * topic.height / topic.width )];
        
        _pictureView.image = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
    }];
    
}
@end
